Ext.onReady(function() {
	var RES_PATH = parent.RES_PATH;
	var CTX_PATH = parent.CTX_PATH;
	
	Ext.BLANK_IMAGE_URL = RES_PATH+'/js/ext2/resources/images/default/s.gif';
	Ext.QuickTips.init();
	Ext.getDom("theme").href = parent.Ext.getDom("theme").href;
	
	var roleTypeStore = new Ext.data.SimpleStore({
		fields : ["value", "text"],
		data : [['0', '固话角色'],['1', '普通角色']]
	});
	var roleTypeCombo = new Ext.form.ComboBox({
		store : roleTypeStore,
		emptyText : '请选择...',
		valueField : "value",
		displayField : "text",
		mode : 'local',
		forceSelection : true,
		hiddenName : 'role_type',
		editable : false,
		triggerAction : 'all',
		allowBlank : false,
		fieldLabel : '角色类型',
		selectOnFocus : true,
		name : 'sex',
		anchor : '80%'
	});
	var roleForm=new Ext.form.FormPanel({
    	reader : new Ext.data.JsonReader({
			root : 'root',
			successProperty : 'success'
		}, [{
			name : 'role_name',
			mapping : 'role_name'
		}, {
			name : 'remark',
			mapping : 'remark'
		}, {
			name : 'role_type',
			mapping : 'role_type'
		}]
		),
		bodyStyle:'padding: 10',
		border : false,
		labelWidth:60, 
		defaultType: 'textfield',
		autoHeight: true,
		method: 'POST',
		items: [
			{
				xtype: 'hidden',
				name:'role_id'
			},{
				fieldLabel:'角色名称',
				name:'role_name',
				anchor : '95%',
				maxLength: 20,
				allowBlank:false
			}, roleTypeCombo, {
				xtype: "textarea",
				fieldLabel:'描述',
				maxLength: 100,
				name:'remark',
				anchor : '95%'
			}
		]
    });
		
	var roleWin = new Ext.Window({
		title : '角色管理',
		width : 350,
		closable:true, //右上关闭按钮
		collapsible:true, //窗口上下拉升
		autoHeight: true,
		closeAction:'hide',
		//autoDestroy:true, //自动销毁
		iconCls : 'add',
		resizable:false, //可改变大小
		modal :true,
		layout:"fit",
		plain:false,//主题背景，true为透明
		//bodyStyle : 'padding:1px;',
		buttonAlign : 'center',
		items : roleForm,
		buttons : [{
			text : '保存',
			id : 'btnSave',
			handler : function() {
				if (roleForm.form.isValid()) {
					var url = "";
					if(isEmpty(roleForm.form.getValues()["role_id"])){
						url = CTX_PATH + '/topic/exec/roleCreate';
					}else{
						url = CTX_PATH + '/topic/exec/roleModify';
					}
					roleForm.form.doAction('submit', {
						url : url,
						method : 'post',
						waitTitle : '提示信息',
						waitMsg : '正在提交....',
						success : function(form, action) {
							if(action.result.result == 1){
								Ext.Msg.alert('提示信息',action.result.resultInfo);
								roleWin.hide();
								gridStore.reload();
							}else if(action.result.result == 2){
								Ext.Msg.alert('提示信息', action.result.resultInfo + "[" +action.result.validate.join(",")+"]");
							}else{
								Ext.Msg.alert('提示信息', action.result.result+"----"+action.result.resultInfo);
							}							
						}, failure : function() {
							Ext.Msg.alert('提示信息', '操作失败，请联系系统管理员！');
						}
					});
				}else{
					Ext.Msg.alert('提示信息', '您必须按规定填写后才可以继续操作！');
				}
			}
		}, {
			text : '关闭', 
			handler : function(){
				roleWin.hide();
			}
		}]		
	});
	
	var gridStore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: CTX_PATH + '/topic/ajax/getRoleListData',
			method: 'POST'
		}),
        reader: new Ext.data.JsonReader({
		    		id: 'role_id',
		    		fields : [
		    			{name : 'role_id', type : 'int' },
		    			{name : 'role_name', type : 'string' },
		    			{name : 'role_type', type : 'string' }
		    		]
		    	})
    });
	gridStore.load();
	
	var grid = new Ext.grid.GridPanel({
    	store: gridStore,
    	region : 'west',
        border:false,
        loadMask: true,
        split : true,
        columns : [
        	new Ext.grid.RowNumberer(),
	        {
	         	header: "角色名称", 
	         	width: 120, 
	         	dataIndex: 'role_name', 
	         	sortable : true
	        },{
	         	header: "角色类型", 
	         	width: 120, 
	         	dataIndex: 'role_type', 
	         	sortable : true
	        }
        ],
        viewConfig: {
            forceFit:true
        },
        loadMask: true,
        tbar: [{
            text: '添加',
			iconCls:'add',
			id : '130',
			disabled : true,
            handler : function(){
            	roleForm.form.reset();
            	roleWin.show();
			}
	    },'-', {
            text: '修改',
			iconCls: 'edit',
			id : '131',
			disabled : true,
            handler : function(){
            	var record = grid.getSelectionModel().getSelected();
                if(!record){
					Ext.Msg.alert("提示","请先选择要编辑的行!");
					return;
				} else{
					roleWin.show();
					roleForm.form.doAction('load',{
						url: CTX_PATH + '/topic/load/getRoleObj',
						params:{'roleId' : record.id},
						method : 'post',
						waitTitle : '提示信息',
						waitMsg : '正在载入...',
						success : function() {
							
						},
						failure : function() {
							Ext.Msg.alert('提示信息', '加载失败，请联系系统管理员！');
						}
					});
				}
			}
	    },'-',{
            text: '删除',
			iconCls: 'remove',
			disabled : true,
			id : '132',
            handler : function(){
                var record = grid.getSelectionModel().getSelected();// 返回值为 Record 类型
                if(!record){
					Ext.Msg.alert("提示","请先选择要删除的行!");
					return;
				}else {
					Ext.MessageBox.confirm("删除确认", "您确定删除本记录吗？", function(flag) {
						if (flag == "yes") {
							viewport.getEl().mask("正在删除...", "x-mask-loading");
							Ext.Ajax.request({
								url : CTX_PATH + '/topic/exec/roleDel',
								params : {
									'roleId' : record.id
								},
								method : 'post',
								success : function(result) {
									var responseObj = Ext.util.JSON.decode(result.responseText);
									viewport.getEl().unmask();
									if (responseObj.result == 1) {
										gridStore.reload();
										Ext.Msg.alert("提示", responseObj.resultInfo);
				                    }else if(action.result.result == 2){
										Ext.Msg.alert('提示信息', action.result.resultInfo + "[" +action.result.validate.join(",")+"]");
									}else{
										Ext.Msg.alert('提示信息', action.result.result+"----"+action.result.resultInfo);
									}
								},
								failure : function() {
									viewport.getEl().unmask();
									Ext.Msg.alert("错误提示", "删除记录失败!");
								}
							});
						}
					});
				}				
        	}
        },'-',{
            text: '分配权限',
			iconCls:'add',
			id : '122',
			disabled : true,
            handler : function(){
            	var record = grid.getSelectionModel().getSelected();
                if(!record){
					Ext.Msg.alert("提示","请先选择要操作的角色!");
					return;
				} else{
            		document.getElementById("roleRightFrame").src = CTX_PATH +"/topic/toRoleRightConf?roleId="+record.id;
            	}
			}
	    }]
    });
	
	var viewport = new Ext.Viewport({
		layout : 'border',
		items : [grid, 
		{
			region : 'center',
			border : false,
			split : true,
			layout : 'fit',
			html:'<iframe id="roleRightFrame" name="roleRightFrame" frameborder="0" src="'+CTX_PATH+'/tip.jsp?flag=selRole" width="100%" height="100%" scrolling="auto"></iframe>'
		}]
	});
	viewport.render();	
	parent.verifyUserBtn(Ext.getCmp);
});